package 品牌管理;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class 查询单个品牌 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的品牌id:");
        int id4Query = sc.nextInt();
        //泛型
        ArrayList<Brand> brandList = new ArrayList<>();
        Brand b=null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2链接数据库
            String url = "jdbc:mysql://localhost/db_jdbc_demo?jdbc:mysql://IP地址:3306/数据库名称?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
            String user = "root";
            String pass = "200233";
            conn = DriverManager.getConnection(url, user, pass);
            //3.书写sql语句
            String sql = "SELECT * FROM t_brand WHERE id = ?";
            //4.创建语句对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id4Query);
            rs = pstmt.executeQuery();
            //5.提出结果
            while (rs.next()) {
                int id = rs.getInt("id");
                String brandName = rs.getString("brand_name");
                String companyName = rs.getString("company_name");
                int ordered = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
                b = new Brand(id,brandName,companyName,
                        ordered,description,status);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.关闭链接
            try {
                rs.close();
                pstmt.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        System.out.println("所查品牌是：");
        System.out.println(b);
    }
}
